package com.lanyam.travel.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanyam.travel.base.dao.GoodsDateMapper;
import com.lanyam.travel.base.entity.GoodsDate;
import com.lanyam.travel.base.service.IGoodsDateService;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rosson
 * @since 2020-01-29
 */
@Service
public class GoodsDateServiceImpl implements IGoodsDateService {

	Logger logger = Logger.getLogger(GoodsDateServiceImpl.class);
	
	@Autowired
	private GoodsDateMapper goodsDateMapper;
	@Override
	public Date getLatestDate(String productId) {
		// TODO Auto-generated method stub
		Date date = goodsDateMapper.queryMaxDate(productId);
		return date;
	}
	@Override
	public void batchAddGoodsDates(List<GoodsDate> addGoodsDateList) {
		// TODO Auto-generated method stub
		for (GoodsDate goodsDate : addGoodsDateList) {
			goodsDateMapper.insert(goodsDate);
		}
	}
	@Override
	public List<GoodsDate> queryGoodsDateInMouth(String productId, Date startDate) {
		// TODO Auto-generated method stub
		QueryWrapper<GoodsDate> wrapper = new QueryWrapper<GoodsDate>();
		wrapper.eq("goods_id", productId).ge("the_date", startDate);
		List<GoodsDate> goodsDateInMouth = goodsDateMapper.selectList(wrapper);
		return goodsDateInMouth;
	}
	@Override
	public void updateGoodsDate(GoodsDate goodDate) {
		// TODO Auto-generated method stub
		goodsDateMapper.updateById(goodDate);
	}
	@Override
	public GoodsDate queryByGoodsIdAndDay(String productId, Date bookDate) {
		// TODO Auto-generated method stub
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(bookDate);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND,0);
		Date startDate = calendar.getTime();
		calendar.add(Calendar.DAY_OF_YEAR, 0);
		Date endDate = calendar.getTime();
		QueryWrapper<GoodsDate> wrapper = new QueryWrapper<GoodsDate>();
		wrapper.eq("goods_id", productId).eq("the_date", startDate);
		List<GoodsDate> selectList = goodsDateMapper.selectList(wrapper);
		if(null != selectList && selectList.size() > 0)
		{
			return selectList.get(0);
		}
		return null;
	}
	@Override
	public void deleteGoodsDateById(Integer id) {
		// TODO Auto-generated method stub
		goodsDateMapper.deleteById(id);
	}
	@Override
	public void batchUpdateGoodsDate(List<GoodsDate> goodsDateList) {
		// TODO Auto-generated method stub
		if(null != goodsDateList && goodsDateList.size() > 0)
		{
			for (GoodsDate goodsDate : goodsDateList) {
				goodsDateMapper.updateById(goodsDate);
			}
		}
	}
	@Override
	public List<GoodsDate> queryGoodsDateInDays(String productId, String startDate, String endDate) {
		// TODO Auto-generated method stub
		try {
			startDate = startDate.replace("/","-");
			endDate = endDate.replace("/","-");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date start = sdf.parse(startDate);
			Date end = sdf.parse(endDate);
			QueryWrapper<GoodsDate> wrapper = new QueryWrapper<GoodsDate>();
			wrapper.eq("goods_id", productId);
			wrapper.between("the_date", start, end);
			List<GoodsDate> selectList = goodsDateMapper.selectList(wrapper);
			return selectList;
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("queryGoodsDateInDays:parse date error");
		}
		
		return null;
	}
	
	@Override
	public List<GoodsDate> getNextMonthRemaidGood(String id, Date time) {
		// TODO Auto-generated method stub
		QueryWrapper<GoodsDate> wrapper = new QueryWrapper<GoodsDate>();
		wrapper.eq("goods_id", id).ge("the_date", time);
		List<GoodsDate> goodsDateInMouth = goodsDateMapper.selectList(wrapper);
		return goodsDateInMouth;
	}
	@Override
	public List<GoodsDate> queryGoodsDateByMouth(String id, String month) {
		// TODO Auto-generated method stub
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
			Date monthDate = sdf.parse(month);
			List<GoodsDate> goodsDateInMouth = goodsDateMapper.queryGoodsDateByMouth(id,monthDate);
			return goodsDateInMouth;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	
}
